﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WSNs
{
    public partial class Form_TopoAndAcq : Form
    {
        public Action<CtlNode> ReConfigAcqEvent;
        private Point _p;
        private CtlNode _node;
        public Form_TopoAndAcq(Point p,CtlNode node)
        {
            _p = p;
            _node = node;
            InitializeComponent();
        }

        private void Form_TopoAndAcq_Load(object sender, EventArgs e)
        {
            switch (_node.NodeType)
            {
                case NodesType.Router:
                case NodesType.Coordinator:
                    gb_acqParm.Visible = false;
                    this.Height = gb_netParm.Height + btn_cancel.Height*3;
                    break;
                case NodesType.EndNode:
                    gb_acqParm.Visible = true;
                    this.Height = gb_netParm.Height + btn_cancel.Height*3 + gb_acqParm.Height;
                    break;
            }
            if (_p.Y + this.Height > SystemInformation.WorkingArea.Height)
                this.Location = new Point(_p.X, SystemInformation.WorkingArea.Height - this.Height);
            else
                this.Location = _p;

            try
            {
                tb_nodeID.Text = _node.NodeID.ToString();
                tb_faternode.Text = _node.FatherNode.ToString();
                tb_fileID.Text = _node.AcqStructure.FileID.ToString();
                tb_length.Text = (_node.AcqStructure.AcqLength / _node.AcqStructure.Frequency / 3).ToString("f2");
                tb_trigger.Text = _node.AcqStructure.TrigerV.ToString();
                cb_couplingmode.SelectedIndex = (int)_node.AcqStructure.CoupleMode - 1;
                cb_measuerrange.SelectedIndex = _node.AcqStructure.MeasuerRange;
                cb_samplingmode.SelectedIndex = (int) _node.AcqStructure.AcqMode - 1;

                tb_samplingrate.Text = _node.AcqStructure.Frequency.ToString();
                cb_storage.SelectedIndex = (int) _node.AcqStructure.StorageLocation - 1;
                tb_sensitivity.Text = _node.Sensitivity.ToString();
            }
            catch
            {
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            CtlNode node = new CtlNode();
            AcqStructure acqStruct = new AcqStructure();
            if (_node.NodeType == NodesType.EndNode)
            {
                node.NodeType = NodesType.EndNode;
                try
                {
                    acqStruct.AcqMode = (AcqMode) (cb_samplingmode.SelectedIndex + 1);
                    acqStruct.NodeID = byte.Parse(tb_nodeID.Text);
                    acqStruct.FileID = byte.Parse(tb_fileID.Text);
                    acqStruct.CoupleMode = (CoupleMode) (cb_couplingmode.SelectedIndex + 1);

                    acqStruct.Frequency = float.Parse(tb_samplingrate.Text);
                    acqStruct.AcqLength = (uint)(Convert.ToDouble(tb_length.Text) * acqStruct.Frequency * 3);

                    acqStruct.MeasuerRange = (byte) (cb_measuerrange.SelectedIndex);
                    acqStruct.TrigerV = byte.Parse(tb_trigger.Text);
                    acqStruct.StorageLocation = (StorageLocation) (cb_storage.SelectedIndex + 1);                    
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                    return;
                }
            }
            node.AcqStructure = acqStruct;
            node.FatherNode = int.Parse(tb_faternode.Text);
            node.Sensitivity = double.Parse(tb_sensitivity.Text);
            ReConfigAcqEvent(node);
            this.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
